package com.springboot.service.impl;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.springboot.entity.News;
import com.springboot.repository.NewsDao;
import com.springboot.service.NewsService;
@Service
public class NewsServiceImpl implements NewsService
{
    @Autowired
    private NewsDao newsDao;

    @Override
    public void save(News news)
    {
        newsDao.save(news);
    }

    @Override
    public News getNewsById(Long id)
    {
        return newsDao.findOne(id);
    }

    @Override
    public List<News> findAll(News news)
    {
        return newsDao.findAll(new Specification<News>()
        {
            @Override
            public Predicate toPredicate(Root<News> root, CriteriaQuery<?> query, CriteriaBuilder cb)
            {
                Path<String> title = root.get("title");
                Path<String> content = root.get("content");
                query.where(cb.like(title, news.getTitle()), cb.like(content, news.getContent()));
                return null;
            }
        });
    }

    @Override
    public void deleteNews(Long id)
    {
        newsDao.delete(id);

    }

    @Override
    public void deleteNes(List<Long> ids)
    {
//        newsDao.deleteAll(ids);
    }

}
